/**
 * 
 */
package co.fitstart.mobile.dao.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.user.User;
import co.fitstart.entity.user.UserAddress;

/**
 * @author Guangli Yan
 *
 */
@Repository("userAddressDaoSupport")
public class UserAddressDaoSupport {

	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	/**
	 * @param user
	 */
	@SuppressWarnings("unchecked")
	public List<UserAddress> list(User user) {
		
		String sentence = "FROM UserAddress WHERE disabled = false AND user = :user ORDER BY createTime DESC";

		Map<String, Object> parameters = new HashMap<>();
		
		parameters.put("user", user);
		
		List<UserAddress> result = genericDaoSupport.searchForList(sentence, parameters);
		
		return result;
	}

	/**
	 * @param user
	 */
	@SuppressWarnings("rawtypes")
	public UserAddress loadUserDefaultAddress(User user) {
		
		String sentence = "FROM UserAddress WHERE disabled = false AND defaultAddress = true AND user = :user ";
		
		Map<String, Object> parameters = new HashMap<>();
		
		parameters.put("user", user);
		
		List list = genericDaoSupport.searchForList(sentence, parameters);
		
		UserAddress userAddress = null;
		
		if (list.size() > 0) {
			
			userAddress = (UserAddress) list.get(0);
			
		}
		return userAddress;
	}
}
